package com.sec.etech.nc.service;

import com.sec.etech.common.service.CommonService;
import com.sec.etech.nc.model.Nc;
import org.openbpm.sys.util.ContextUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NcHelper extends CommonService {

    @Resource
    NcService ncService;

    /**
     * 判读用户是否是该NC任务的特定用户（admin:系统管理员；admin4nc：平台管理员；manager：编程组班组长；adder：添加权限；amender：修改权限）
     * designer：编制者；checker：校对；reviewer：审核；
     * ncid=0表示全平台范围权限
     *
     * @return
     */
    public boolean checkPower(String ncid, String role) {
        if (isAdmin()) {
            return true;
        }
        Long currentUserId = Long.parseLong(ContextUtil.getCurrentUserId());
        if ("admin4nc".equals(role)) {
            return hasRole("nc_admin");
        } else if ("manager".equals(role)) {
            return isManager("73");
        } else if ("adder".equals(role)) {
            return (hasRole("nc_add") || hasRole("nc_add1")|| hasRole("nc_add2")|| hasRole("nc_add3")|| hasRole("nc_add4"));
        } else if ("amender".equals(role)) {
            return hasRole("nc_modify");
        } else if ("admin4plan".equals(role)) {
            return hasRole("plan_admin");
        } else {
            Nc nc = ncService.getById(ncid);
            if (nc != null) {
                if ("designer".equals(role)) {
                    return currentUserId.equals(nc.getDesigneruid());
                } else if ("checker".equals(role)) {
                    return currentUserId.equals(nc.getCheckeruid());
                } else if ("reviewer".equals(role)) {
                    return currentUserId.equals(nc.getRevieweruid());
                }
            }
        }
        return false;
    }
}
